package gui.newgui.archerdbeditor;

import gui.newgui.IController;
import gui.newgui.IArCompGuiComponent;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import table.models.ArcherDBTableModel;
import table.models.ParticipantsTableModelRenderer;
import core.CustomIcons;
import core.GuiComponentLabels;

public class NewArcherDBEditor extends javax.swing.JFrame implements IArCompGuiComponent {
	private final IController controller;
	private JPanel tableWrapperPanel;
	private JPanel optionsWrapperPanel;
	private JTextField nameTextField;
	private JScrollPane jScrollPane1;
	private JCheckBox tournamentParticipationCheckBox;

	public JCheckBox getTournamentParticipationCheckBox() {
		return this.tournamentParticipationCheckBox;
	}

	private JTable playerTable;
	private JButton closeButton;
	private JButton saveButton;
	private JCheckBox makeEditableCheckBox;
	private JButton addPlayerButton;
	private JButton deletePlayerButton;
	private JCheckBox countryRatingCheckBox;
	private JCheckBox licenseCheckBox;
	private JComboBox genderDropDown;
	private JTextField birthDateTextField;
	private JComboBox clubDropDown;
	private JComboBox ageClassDropDown;
	private JComboBox bowCathegoryDropDown;
	private JComboBox countryDropDown;

	public JTextField getNameTextField() {
		return this.nameTextField;
	}

	public JTable getPlayerTable() {
		return this.playerTable;
	}

	public JCheckBox getMakeEditableCheckBox() {
		return this.makeEditableCheckBox;
	}

	public JCheckBox getCountryRatingCheckBox() {
		return this.countryRatingCheckBox;
	}

	public JCheckBox getLicenseCheckBox() {
		return this.licenseCheckBox;
	}

	public JComboBox getGenderDropDown() {
		return this.genderDropDown;
	}

	public JTextField getBirthDateTextField() {
		return this.birthDateTextField;
	}

	public JComboBox getClubDropDown() {
		return this.clubDropDown;
	}

	public JComboBox getAgeClassDropDown() {
		return this.ageClassDropDown;
	}

	public JComboBox getBowCathegoryDropDown() {
		return this.bowCathegoryDropDown;
	}

	public JComboBox getCountryDropDown() {
		return this.countryDropDown;
	}

	public JComboBox getNationDropDown() {
		return this.nationDropDown;
	}

	public JTextField getSurNameTextField() {
		return this.surNameTextField;
	}

	private JComboBox nationDropDown;
	private JTextField surNameTextField;
	private JPanel dbEntryWrapperPanel;
	private final KeyListener keyListener;
	private final MouseListener mouseListener;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String [] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				NewArcherDBEditor inst = new NewArcherDBEditor();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public NewArcherDBEditor() {
		super();
		this.controller = new NewArcherDBEditorController(this);
		this.keyListener = new NewArcherDbEditorKeyListener(this);
		this.mouseListener = new NewArcherDbEditorMouseListener(this);
		this.initGUI();
		this.controller.updateTable();
		((NewArcherDBEditorController) this.controller).setDropDownMenus();
	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout(this.getContentPane());
			this.getContentPane().setLayout(thisLayout);
			this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				this.tableWrapperPanel = new JPanel();
				BorderLayout tableWrapperPanelLayout = new BorderLayout();
				this.tableWrapperPanel.setLayout(tableWrapperPanelLayout);
				{
					this.jScrollPane1 = new JScrollPane();
					this.tableWrapperPanel.add(this.jScrollPane1, BorderLayout.CENTER);
					this.jScrollPane1.setPreferredSize(new java.awt.Dimension(537, 617));
					{
						this.playerTable = new JTable();
						this.playerTable.addKeyListener(this.keyListener);
						this.playerTable.addMouseListener(this.mouseListener);
						this.playerTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
						this.playerTable.setModel((new ArcherDBTableModel(new String [] { GuiComponentLabels.ID, GuiComponentLabels.NAME, GuiComponentLabels.SURNAME, GuiComponentLabels.NATION,
								GuiComponentLabels.COUNTRY, GuiComponentLabels.BOW_CATHEGORY, GuiComponentLabels.AGE_CLASS, GuiComponentLabels.CLUB, GuiComponentLabels.GENDER,
								GuiComponentLabels.LICENSE, GuiComponentLabels.COUNTRY_RATING, GuiComponentLabels.BIRTHDATE })));
						this.jScrollPane1.setViewportView(this.playerTable);
						this.playerTable.setAutoCreateRowSorter(true);
						this.playerTable.setDefaultRenderer(Object.class, new ParticipantsTableModelRenderer());
						this.playerTable.setDefaultRenderer(Integer.class, new ParticipantsTableModelRenderer());
						this.playerTable.setDefaultRenderer(Boolean.class, new ParticipantsTableModelRenderer());
					}
				}
			}
			{
				this.dbEntryWrapperPanel = new JPanel();
				GroupLayout dbEntryWrapperPanelLayout = new GroupLayout(this.dbEntryWrapperPanel);
				this.dbEntryWrapperPanel.setLayout(dbEntryWrapperPanelLayout);
				this.dbEntryWrapperPanel.setBorder(BorderFactory.createTitledBorder(GuiComponentLabels.DATA));
				{
					this.nameTextField = new JTextField();
					this.nameTextField.setToolTipText(GuiComponentLabels.NAME);
				}
				{
					this.surNameTextField = new JTextField();
					this.surNameTextField.setToolTipText(GuiComponentLabels.SURNAME);
				}
				{
					this.nationDropDown = new JComboBox();
					this.nationDropDown.setToolTipText(GuiComponentLabels.NATION);
				}
				{
					this.countryDropDown = new JComboBox();
					this.countryDropDown.setToolTipText(GuiComponentLabels.COUNTRY);
				}
				{
					this.bowCathegoryDropDown = new JComboBox();
					this.bowCathegoryDropDown.setToolTipText(GuiComponentLabels.BOW_CATHEGORY);
				}
				{
					this.ageClassDropDown = new JComboBox();
					this.ageClassDropDown.setToolTipText(GuiComponentLabels.AGE_CLASS);
				}
				{
					this.clubDropDown = new JComboBox();
					this.clubDropDown.setToolTipText(GuiComponentLabels.CLUB);
				}
				{
					this.birthDateTextField = new JTextField();
					this.birthDateTextField.setToolTipText(GuiComponentLabels.BIRTHDATE);
				}
				{
					this.genderDropDown = new JComboBox();
					this.genderDropDown.setToolTipText(GuiComponentLabels.GENDER);
				}
				{
					this.licenseCheckBox = new JCheckBox();
					this.licenseCheckBox.setText(GuiComponentLabels.LICENSE);
				}
				{
					this.countryRatingCheckBox = new JCheckBox();
					this.countryRatingCheckBox.setText(GuiComponentLabels.COUNTRY_RATING);
				}
				{
					this.tournamentParticipationCheckBox = new JCheckBox();
					this.tournamentParticipationCheckBox.setText(GuiComponentLabels.PARTICIPATES_IN_CURRENT);
				}
				dbEntryWrapperPanelLayout
						.setHorizontalGroup(dbEntryWrapperPanelLayout
								.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										dbEntryWrapperPanelLayout
												.createParallelGroup()
												.addComponent(this.nameTextField, GroupLayout.Alignment.LEADING, 0, 166, Short.MAX_VALUE)
												.addComponent(this.surNameTextField, GroupLayout.Alignment.LEADING, 0, 166, Short.MAX_VALUE)
												.addComponent(this.nationDropDown, GroupLayout.Alignment.LEADING, 0, 166, Short.MAX_VALUE)
												.addComponent(this.countryDropDown, GroupLayout.Alignment.LEADING, 0, 166, Short.MAX_VALUE)
												.addComponent(this.bowCathegoryDropDown, GroupLayout.Alignment.LEADING, 0, 166, Short.MAX_VALUE)
												.addComponent(this.ageClassDropDown, GroupLayout.Alignment.LEADING, 0, 166, Short.MAX_VALUE)
												.addComponent(this.clubDropDown, GroupLayout.Alignment.LEADING, 0, 166, Short.MAX_VALUE)
												.addComponent(this.birthDateTextField, GroupLayout.Alignment.LEADING, 0, 166, Short.MAX_VALUE)
												.addComponent(this.genderDropDown, GroupLayout.Alignment.LEADING, 0, 166, Short.MAX_VALUE)
												.addComponent(this.licenseCheckBox, GroupLayout.Alignment.LEADING, 0, 166, Short.MAX_VALUE)
												.addComponent(this.countryRatingCheckBox, GroupLayout.Alignment.LEADING, 0, 166, Short.MAX_VALUE)
												.addGroup(
														dbEntryWrapperPanelLayout.createSequentialGroup()
																.addComponent(this.tournamentParticipationCheckBox, GroupLayout.PREFERRED_SIZE, 166, GroupLayout.PREFERRED_SIZE)
																.addGap(0, 0, Short.MAX_VALUE))).addContainerGap());
				dbEntryWrapperPanelLayout.setVerticalGroup(dbEntryWrapperPanelLayout.createSequentialGroup()
						.addComponent(this.nameTextField, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.surNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.nationDropDown, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.countryDropDown, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.bowCathegoryDropDown, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.ageClassDropDown, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.clubDropDown, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.birthDateTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.genderDropDown, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.licenseCheckBox, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.countryRatingCheckBox, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.tournamentParticipationCheckBox, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
			}
			{
				this.optionsWrapperPanel = new JPanel();
				GroupLayout optionsWrapperPanelLayout = new GroupLayout(this.optionsWrapperPanel);
				this.optionsWrapperPanel.setLayout(optionsWrapperPanelLayout);
				this.optionsWrapperPanel.setBorder(BorderFactory.createTitledBorder(GuiComponentLabels.OPTIONS));
				{
					this.deletePlayerButton = new JButton();
					this.deletePlayerButton.setToolTipText(GuiComponentLabels.DELETE_PLAYER_TOOLTIP);
					this.deletePlayerButton.setText(GuiComponentLabels.DELETE_PLAYER_BUTTON);
					this.deletePlayerButton.setIcon(CustomIcons.deletePlayer);
					this.deletePlayerButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							((NewArcherDBEditorController) NewArcherDBEditor.this.controller).deleteEntry();
						}
					});
				}
				{
					this.addPlayerButton = new JButton();
					this.addPlayerButton.setText(GuiComponentLabels.ADD_PLAYER_BUTTON);
					this.addPlayerButton.setIcon(CustomIcons.addPlayer);
					this.addPlayerButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							((NewArcherDBEditorController) NewArcherDBEditor.this.controller).saveEntry(false);
						}
					});
				}
				{
					this.makeEditableCheckBox = new JCheckBox();
					this.makeEditableCheckBox.addMouseListener(this.mouseListener);
					this.makeEditableCheckBox.addKeyListener(this.keyListener);
					this.makeEditableCheckBox.setText(GuiComponentLabels.EDIT_MODE);
				}
				{
					this.saveButton = new JButton();
					this.saveButton.setText(GuiComponentLabels.SAVE_BUTTON);
					this.saveButton.setIcon(CustomIcons.save);
					this.saveButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							((NewArcherDBEditorController) NewArcherDBEditor.this.controller).saveEntry(true);
						}
					});
				}
				{
					this.closeButton = new JButton();
					this.closeButton.setText(GuiComponentLabels.CLOSE_BUTTON);
					this.closeButton.setIcon(CustomIcons.closeIcon);
					this.closeButton.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							NewArcherDBEditor.this.dispose();
						}
					});
				}
				optionsWrapperPanelLayout.setHorizontalGroup(optionsWrapperPanelLayout
						.createSequentialGroup()
						.addContainerGap()
						.addGroup(
								optionsWrapperPanelLayout.createParallelGroup().addComponent(this.addPlayerButton, GroupLayout.Alignment.LEADING, 0, 167, Short.MAX_VALUE)
										.addComponent(this.deletePlayerButton, GroupLayout.Alignment.LEADING, 0, 167, Short.MAX_VALUE)
										.addComponent(this.makeEditableCheckBox, GroupLayout.Alignment.LEADING, 0, 167, Short.MAX_VALUE)
										.addComponent(this.saveButton, GroupLayout.Alignment.LEADING, 0, 167, Short.MAX_VALUE)
										.addComponent(this.closeButton, GroupLayout.Alignment.LEADING, 0, 167, Short.MAX_VALUE)).addContainerGap());
				optionsWrapperPanelLayout.setVerticalGroup(optionsWrapperPanelLayout.createSequentialGroup()
						.addComponent(this.makeEditableCheckBox, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.deletePlayerButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
						.addComponent(this.addPlayerButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.saveButton, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.closeButton, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(29, Short.MAX_VALUE));
			}
			thisLayout.setVerticalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							thisLayout
									.createParallelGroup()
									.addGroup(
											GroupLayout.Alignment.LEADING,
											thisLayout.createSequentialGroup().addComponent(this.dbEntryWrapperPanel, GroupLayout.PREFERRED_SIZE, 421, GroupLayout.PREFERRED_SIZE)
													.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.optionsWrapperPanel, 0, 234, Short.MAX_VALUE))
									.addComponent(this.tableWrapperPanel, GroupLayout.Alignment.LEADING, 0, 667, Short.MAX_VALUE)).addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							thisLayout.createParallelGroup().addComponent(this.dbEntryWrapperPanel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE)
									.addComponent(this.optionsWrapperPanel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 200, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(this.tableWrapperPanel, 0, 537, Short.MAX_VALUE).addContainerGap());
			this.pack();
			this.setSize(789, 729);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

	@Override
	public IController getController() {
		return this.controller;
	}

	public JTable getTable() {
		return this.playerTable;
	}

}
